//
//  AIHomeMainView.m
//  Mia
//
//  Created by Vill on 2024/1/18.
//

#import "AIHomeMainView.h"
#import "AIHomeWalletBtn.h"    // 钱包按钮
#import "AIHomeUpBottomBtn.h"

@interface AIHomeMainView ()

@property (nonatomic, strong) UIView *topView;      // 顶部View
@property (nonatomic, strong) UIButton *rightBtn;   // 更多按钮
@property (nonatomic, strong) UIButton *vipPlus;    // vip按钮
@property (nonatomic, strong) AIHomeWalletBtn *walletBtn;  //钱包按钮
//@property (nonatomic, strong) AIHomeUpBottomBtn *diviBtn;    //星座
//@property (nonatomic, strong) AIHomeUpBottomBtn *tarorBtn;   //塔罗牌

@property (nonatomic, strong) AIHomeUpBottomBtn *arBtn;
@property (nonatomic, strong) AIHomeUpBottomBtn *factsBtn;
@property (nonatomic, strong) AIHomeUpBottomBtn *diaryBtn;

@end

@implementation AIHomeMainView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self == [super initWithFrame:frame]){
        
        // 顶部view
        [self addSubview:self.topView];
        [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.trailing.mas_equalTo(self);
            make.height.mas_equalTo(KNavigationBarHeight + PTW(20));
        }];
        // 更多按钮
        [self addSubview:self.rightBtn];
        [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(PTW(34));
            make.top.mas_equalTo(self).mas_offset(KStatusBarHeight + PTW(5));
            make.trailing.mas_equalTo(self).mas_offset(-PTW(12.5));
        }];
        // vip按钮
        [self addSubview:self.vipPlus];
        [self.vipPlus mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(PTW(83), PTW(34)));
            make.centerY.mas_equalTo(self.rightBtn);
            make.trailing.mas_equalTo(self.rightBtn.mas_leading).mas_offset(-PTW(10));
        }];
        // 钱包
        [self addSubview:self.walletBtn];
        [self.walletBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_greaterThanOrEqualTo(PTW(60));
            make.height.mas_equalTo(PTW(34));
            make.centerY.mas_equalTo(self.rightBtn);
            make.leading.mas_equalTo(self).mas_offset(PTW(12.5));
        }];
        AIUserInfoModel *model = [[AIUserInfoManager shareManager] userInfo];
        [self.walletBtn setWallet:model.coinBalance];
        
//        // 星座
//        [self addSubview:self.diviBtn];
//        [self.diviBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.mas_equalTo(CGSizeMake(PTW(72), PTW(66)));
//            make.trailing.mas_equalTo(self).mas_offset(-PTW(4));
//            make.top.mas_equalTo(self.rightBtn.mas_bottom).mas_offset(PTW(30));
//        }];
//        
//        // 塔罗牌
//        [self addSubview:self.tarorBtn];
//        [self.tarorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.mas_equalTo(CGSizeMake(PTW(72), PTW(66)));
//            make.trailing.mas_equalTo(self).mas_offset(-PTW(4));
//            make.top.mas_equalTo(self.diviBtn.mas_bottom).mas_offset(PTW(20));
//        }];
        
        // facts
        [self addSubview:self.factsBtn];
        [self.factsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(PTW(40), PTW(55)));
            make.centerX.mas_equalTo(self);
            make.bottom.mas_equalTo(self).mas_offset(-KBottomSafeHeight-PTW(64));
        }];
        
        // facts
        [self addSubview:self.arBtn];
        [self.arBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(PTW(40), PTW(55)));
            make.centerY.mas_equalTo(self.factsBtn);
            make.trailing.mas_equalTo(self.factsBtn.mas_leading).mas_offset(-PTW(70));
        }];
        
        // diary
        [self addSubview:self.diaryBtn];
        [self.diaryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(PTW(40), PTW(55)));
            make.centerY.mas_equalTo(self.factsBtn);
            make.leading.mas_equalTo(self.factsBtn.mas_trailing).mas_offset(PTW(70));
        }];
        
        [self.topView setGradientBackgroundWithColors:@[[UIColor colorHex:@"#000000" alpha:0.1],[UIColor colorHex:@"#000000" alpha:0]] locations:@[@0,@1] startPoint:CGPointMake(0,0) endPoint:CGPointMake(0, 1)];
        
        // 金币数量改变
        NTFAdd(BalanceUpdateNotification, @selector(balanceChange:));
    }
    return self;
}

#pragma  mark ------------- 金币数量改变 -------------
- (void)balanceChange:(NSNotification *)noti{
    AIUserInfoModel *model = [[AIUserInfoManager shareManager] userInfo];
    [self.walletBtn setWallet:model.coinBalance];
}

#pragma  mark ------------- lazy -------------
- (UIView *)topView{
    if(!_topView){
        _topView = [UIView new];
    }
    return _topView;
}

// 更多
- (UIButton *)rightBtn{
    if(!_rightBtn){
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightBtn setImage:KImaged(@"ai_home_more") forState:UIControlStateNormal];
        [_rightBtn addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}

// vip
-(UIButton *)vipPlus{
    if(!_vipPlus){
        _vipPlus = [UIButton buttonWithType:UIButtonTypeCustom];
        _vipPlus.backgroundColor = [UIColor colorHex:@"#333333" alpha:0.3];
        [_vipPlus setImage:KImaged(@"ai_home_vipLogo") forState:UIControlStateNormal];
        [_vipPlus setTitle:@"Plus" forState:UIControlStateNormal];
        [_vipPlus setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _vipPlus.titleLabel.font = [UIFont fontWithWeight:MTBold size:PTW(20)];
        _vipPlus.layer.cornerRadius = PTW(17);
        _vipPlus.layer.masksToBounds = YES;
        [_vipPlus addTarget:self action:@selector(vipPlusAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _vipPlus;
}

// 钱包
- (AIHomeWalletBtn *)walletBtn{
    if(!_walletBtn){
        _walletBtn = [AIHomeWalletBtn new];
        [_walletBtn addTarget:self action:@selector(walletAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _walletBtn;
}

//// 星座占卜
//- (AIHomeUpBottomBtn *)diviBtn{
//    if(!_diviBtn){
//        _diviBtn = [AIHomeUpBottomBtn new];
//        [_diviBtn setTitle:@"Constellation" withImg:@"ai_home_constel"];
//    }
//    return _diviBtn;
//}
//
//// 塔罗牌
//- (AIHomeUpBottomBtn *)tarorBtn{
//    if(!_tarorBtn){
//        _tarorBtn = [AIHomeUpBottomBtn new];
//        [_tarorBtn setTitle:@"Tarot" withImg:@"ai_home_tarot"];
//    }
//    return _tarorBtn;
//}

// AR
- (AIHomeUpBottomBtn *)arBtn{
    if(!_arBtn){
        _arBtn = [AIHomeUpBottomBtn new];
        [_arBtn setTitle:@"AR" withImg:@"ai_home_ar"];
        [_arBtn addTarget:self action:@selector(arAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _arBtn;
}

// facts
- (AIHomeUpBottomBtn *)factsBtn{
    if(!_factsBtn){
        _factsBtn = [AIHomeUpBottomBtn new];
        [_factsBtn setTitle:@"Facts" withImg:@"ai_home_facts"];
        [_factsBtn addTarget:self action:@selector(factsAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _factsBtn;
}


// Diary
- (AIHomeUpBottomBtn *)diaryBtn{
    if(!_diaryBtn){
        _diaryBtn = [AIHomeUpBottomBtn new];
        [_diaryBtn setTitle:@"Diary" withImg:@"ai_home_diary"];
        [_diaryBtn addTarget:self action:@selector(diaryAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _diaryBtn;
}


- (void)rightAction{
    if (self.homeBtnBlock){
        self.homeBtnBlock(0);
    }
}

- (void)walletAction{
    if (self.homeBtnBlock){
        self.homeBtnBlock(1);
    }
}

- (void)vipPlusAction{
    if (self.homeBtnBlock){
        self.homeBtnBlock(2);
    }
}

- (void)arAction{
    if (self.homeBtnBlock){
        self.homeBtnBlock(3);
    }
}

-(void)factsAction{
    if (self.homeBtnBlock){
        self.homeBtnBlock(4);
    }
}

-(void)diaryAction{
    if (self.homeBtnBlock){
        self.homeBtnBlock(5);
    }
}

@end
